package com.rjwh.dingdong.client.activity.loginregist;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.guide.CaptureActivity;
import com.rjwh.dingdong.client.bean.localbean.ChildInfo;
import com.rjwh.dingdong.client.mvp.presenter.impl.AddBabyBoundPresenter;
import com.rjwh.dingdong.client.mvp.view.AddBabyBoundView;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.ToastUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName AddBabyBoundActivity
 * @Description
 * @date 2016-7-15
 * @history 2016-7-15 author: description:
 */
public class AddBabyBoundActivity extends BaseActivity implements AddBabyBoundView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.add_baby_bound_et)
    EditText addBabyBoundEt;
    private String childid;
    private AddBabyBoundPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_baby_bound_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new AddBabyBoundPresenter(this,this);
    }

    private void initView() {
        initToolbarHelper();
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("绑定宝宝");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick({R.id.add_baby_bound_qr_btn, R.id.add_baby_bound_conform_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_baby_bound_qr_btn:
                // 扫描二维码
                Intent openCameraIntent = new Intent(AddBabyBoundActivity.this, CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
                break;
            case R.id.add_baby_bound_conform_btn:
                doAddBabyCheck();
                break;
        }
    }

    private void doAddBabyCheck() {
        childid = addBabyBoundEt.getText().toString();
        if (!AsStrUtils.isEmpty(childid)) {
            presenter.doAddBabyCheck(childid);
        } else {
            ToastUtil.showToast(this, "请输入id哦");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            LogUtil.d(scanResult);
            if (scanResult != null && scanResult.contains("tiandilixin")) {
                int startIndex = scanResult.indexOf("babyid=");
                String substring = scanResult.substring(startIndex+7);
                LogUtil.e("AddBabyBoundActivity", "substring: " + substring);
                addBabyBoundEt.setText(substring);
            }
        }
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void LoadData(ChildInfo childInfo) {
        String childname = childInfo.getChildname();
        String childavatar = childInfo.getChildavatar();
        Intent intent = new Intent(this, AddBabyBoundCompleteActivity.class);
        intent.putExtra("childid", childid);
        intent.putExtra("childname", childname);
        intent.putExtra("childavatar", childavatar);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
